﻿using System;
using System.IO;
using System.Runtime.Serialization;

namespace _036_数据协定的简单定义
{
    class Program
    {
        static void Main(string[] args)
        {

            using (MemoryStream 实例化内存流=new MemoryStream())
            {
                声明类1 创建实例 = new 声明类1
                {
                    Title = "逐浪软件",
                    Year = 2017,
                    Artist = "Lee Tan",
                    Cover = "05.jpg"
                };

                DataContractSerializer 执行序列化 = new DataContractSerializer(创建实例.GetType());
                执行序列化.WriteObject(实例化内存流, 创建实例);

                //读取并显示结果
                实例化内存流.Position = 0L;
                using (StreamReader 读取=new StreamReader(实例化内存流))
                {
                    string 输出 = 读取.ReadToEnd();
                    Console.WriteLine($"序列化后的内容 如下：\n{输出}");
                }
            }

        }
    }

    [DataContract]
    public class 声明类1
    {
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public string Artist { get; set; }
        [DataMember]
        public int  Year { get; set; }
        [DataMember]
        public string Cover { get; set; }
    }
}
